const App = {
    mixins: [Save],//配置文件，路径/utils/mixins.js
    data() {
        return {
            natural_resources_tags:[],
            natural_resources_management:[]
        };
    },
    mounted() {
        this.getCategory();
        let ls_URL = window.location.href;
        this.nodeKey = getQueryObject(ls_URL, "nodeKey");// 获取 nodeKey 参数
        if (this.nodeKey) {
            this.dataForm.Natural_Resource_Tag_SerialNumber = this.nodeKey;
            this.changeTag(this.nodeKey);
        }
    },
    methods: {
        getCategory() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'natural_resources_tags',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.natural_resources_tags = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'natural_resources_management',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.natural_resources_management = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        changeTag(val) {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                type: "POST",
                data: {
                    Data: 'natural_resources_mapping_tags',
                    Condition: "Natural_Resource_Tag_SerialNumber='" + val + "'",
                },
                success: (data) => {
                    if (data.length > 0) {
                        this.childDataForm = data; // 直接使用 this
                    } else {
                        this.childDataForm = [{}]
                    }
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        async handleSubmit() {
            const allValid = await this.validateAllForms();
            if (allValid) {
                this.loading = true;
                const { add, modify } = this.deal();
                let lJSONObject_DataSet = {
                    Add:{
                        natural_resources_mapping_tags:add
                    },
                    Modify:{
                        natural_resources_mapping_tags:modify
                    }
                }
                let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                if (!lJSONObject_DataSet['Add']) {
                    lJSONObject_DataSet['Add'] = new Object();
                }
                lJSONObject_DataSet['Add']['base_operation_log'] = [{
                    Operation_Type: '0',
                    Operation_Path: this.relativePath,
                    Operation_Main,
                    Operation_Interface: "generalSave",
                    Operation_IP: getIpAddress()
                }]
                submitSave(lJSONObject_DataSet,(res)=>{
                    if (res.Status == 'success') {
                        window.location.href = './index.html'
                    }
                    this.loading = false;
                })
            }
        },
        deal() {    //将待提交的数据中添加和修改分离出来
            let add_Data = [], modify_Data = [];
            for (const i in this.childDataForm) {
                const obj = this.childDataForm[i];
                if (!Object.keys(obj).some(item => item.includes('_Original'))) {
                    obj.Natural_Resource_Tag_SerialNumber = this.dataForm.Natural_Resource_Tag_SerialNumber
                    add_Data.push(obj);
                } else {
                    modify_Data.push(obj);
                }
            }
            return { add: add_Data, modify: modify_Data };
        },
        handleAdd(formName) {
            if (this.action === 'Add') {
                this[formName].push({
                    Natural_Resource_Tag_SerialNumber:this.dataForm.Natural_Resource_Tag_SerialNumber
                })
            }
        },
        handleRemove(formName) {
            if (this[formName].filter(item => item.Row_Status != '1').length > 1) {
                let i = this[formName].filter(item => item.Row_Status != '1').length - 1
                let obj = this[formName].filter(item => item.Row_Status != '1')[i]
                if (!Object.keys(obj).some(item => item.includes('_Original'))) {
                    this[formName].pop();
                } else {
                    this.$confirm('该操作将删除此条数据,是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                    }).then(() => {
                        this[formName].filter(item => item.Row_Status != '1')[i].Row_Status = '1'
                    }).catch(() => {
                        this.$message({
                            type: 'warning',
                            message: '已取消删除'
                        });
                    });
                }
            } else {
                this.$message({
                    message: '不能在删除了',
                    type: 'warning'
                })
            }
        }
    },
};
const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
//自定义组件
app.component('yl-upload', ylUpload);
app.component('yl-select',ylSelect)
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
